﻿using System.Collections.Generic;
using Data;
using Entities;
using System;
using System.Linq;
namespace Logic
{
    public sealed class ReportsLogic
    {
        public Dictionary<string, int> GetSexGlobalVotinReportLogic(int type)
        {
            Dictionary<string, int> dic = new Dictionary<string, int>();

            var sexos = new VoterData().GetListSex();

            foreach (Gender s in sexos)
            {
                var rs = new ReportsData().GetGenericVotinReport(type, s.sex, Convert.ToInt32(s.id));
                rs.ToList().ForEach(x => dic.Add(x.Key, x.Value));
            }

            return dic;
        }

        public Dictionary<string, int> GetCivilStatusGlobalVotinReportLogic(int type)
        {
            Dictionary<string, int> dic = new Dictionary<string, int>();

            var civil = new VoterData().GetListCivilStatus();

            foreach (CivilStatus s in civil)
            {
                var rs = new ReportsData().GetGenericVotinReport(type, s.status, Convert.ToInt32(s.id));
                rs.ToList().ForEach(x => dic.Add(x.Key, x.Value));
            }

            return dic;
        }

        public Dictionary<string, int> GetAgeGlobalVotinReportLogic()
        {
            Dictionary<string, int> masterDic = new Dictionary<string, int>();
            //diccinarios por rango

            Dictionary<string, int> range_20_30 = new Dictionary<string, int>();
            Dictionary<string, int> range_30_40 = new Dictionary<string, int>();
            Dictionary<string, int> range_40_50 = new Dictionary<string, int>();
            Dictionary<string, int> range_50_60 = new Dictionary<string, int>();
            Dictionary<string, int> less_than_20 = new Dictionary<string, int>();
            Dictionary<string, int> greater_than_60 = new Dictionary<string, int>();


            //uf... contadores
            int count_range_20_30 = 0;
            int count_range_30_40 = 0;
            int count_range_40_50 = 0;
            int count_range_50_60 = 0;
            int count_less_than_20 = 0;
            int count_greater_than_60 = 0;


            //Vamos CTM... obtengo todos los votantes
            var listVoter = new VoterData().GetListVoters();

            foreach (Voter v in listVoter)
            {

                var age = DateTime.Now.Year - v.birthDate.Year;

                if (age < 20)
                {
                    count_less_than_20++;
                    continue;
                }
                else
                    if (age >= 20 && age < 30)
                    {
                        count_range_20_30++;
                        continue;
                    }
                    else
                        if (age >= 30 && age < 40)
                        {
                            count_range_30_40++;
                            continue;

                        }
                        else
                            if (age >= 40 && age < 50)
                            {
                                count_range_40_50++;
                                continue;
                            }
                            else
                                if (age >= 50 && age < 60)
                                {
                                    count_range_50_60++;
                                    continue;
                                }
                                else
                                {
                                    count_greater_than_60++;
                                    continue;
                                }
            }


            //Agregar datos a diccionarios por rangos
            range_20_30.Add("20 y 29", count_range_20_30);
            range_30_40.Add("30 y 39 ", count_range_20_30);
            range_40_50.Add("40 y 49 ", count_range_40_50);
            range_50_60.Add("50 y 59", count_range_50_60);
            greater_than_60.Add("Mayor a 60", count_greater_than_60);
            less_than_20.Add("Menor a 20", count_less_than_20);


            // agregar diccionaros por edad al master diccionario

            range_20_30.ToList().ForEach(x => masterDic.Add(x.Key, x.Value));
            range_30_40.ToList().ForEach(x => masterDic.Add(x.Key, x.Value));
            range_40_50.ToList().ForEach(x => masterDic.Add(x.Key, x.Value));
            range_50_60.ToList().ForEach(x => masterDic.Add(x.Key, x.Value));
            greater_than_60.ToList().ForEach(x => masterDic.Add(x.Key, x.Value));
            less_than_20.ToList().ForEach(x => masterDic.Add(x.Key, x.Value));

            return masterDic;
        }


        public Dictionary<string, int> GetGroupGlobalVotinReportLogic(int type)
        {
            Dictionary<string, int> dic = new Dictionary<string, int>();

            var group = new VoterData().GetlistOfGroup();

            foreach (Group s in group)
            {
                var rs = new ReportsData().GetGenericVotinReport(type, s.name, Convert.ToInt32(s.id));
                rs.ToList().ForEach(x => dic.Add(x.Key, x.Value));
            }

            return dic;
        }


        public Dictionary<string, int> GetComunneGlobalVotinReportLogic(int type)
        {
            Dictionary<string, int> dic = new Dictionary<string, int>();
            RegionComunneData comunne = new RegionComunneData();

            List<Voter> listVoter = new VoterData().GetListVoters();

            var idComunneOfVoter = listVoter.Select(x => Convert.ToInt32(x.communeId)).Distinct().ToList();

            foreach (int i in idComunneOfVoter)
            {
                string nameComune = comunne.GetComunneById(i).name.FirstToUpper(); ;
                var rs = new ReportsData().GetGenericVotinReport(type, nameComune, i);
                rs.ToList().ForEach(x => dic.Add(x.Key, x.Value));
            }

            return dic;
        }

    }
}
